# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

"""z1 turret component"""
from __future__ import division, absolute_import
from src import unit, entity, geom, loaders, sprite, views

@loaders.load_props('units/components/z1turret/')
class Model(entity.Entity): # todo: add GenericTurret implementation
    def __init__(self):
        super(Model, self).__init__()
        self._target = None
        self.dir_abs = 0 # absolute dir (parent + self)
    def _set_target(self, target):
        self._target = target
        self.dst_dir = geom.dir_between_px(self.parent.x, self.parent.y, target.x, target.y) - self.parent.dir
        #print 'DST_DIR', self.dst_dir
    target = property(lambda self: self._target, _set_target)
    @property
    def abs_dir(self):
        return self.parent.dir + self.dir
    def fire(self):
        print self.__class__, 'FIRIN'

class View(views.BaseView):
    def __init__(self):
        self.spr = sprite.DirSprite(parent=self)
    def draw(self):
        tmp = self.dir
        self.dir = self.abs_dir
        self.spr.draw()
        self.dir = tmp

class Z1Turret(View, Model):
    object_name = 'Z1 Turret'
    def __init__(self):
        View.__init__(self)
        self._view_dict = self.__dict__.copy()
        Model.__init__(self)
    def __setstate__(self, state):
        self.__init__()
        self.__dict__.update(state)
